package cn.tedu.net;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

//socket通信的客户端
public class Client {
    public static void main(String[] args) throws IOException {
        //本机ip--127.0.0.1
        Socket client = new Socket("127.0.0.1", 8080);
        System.out.println("客户端与服务器连接成功...");
        //开始通信
        BufferedOutputStream out = new BufferedOutputStream(client.getOutputStream());
        System.out.println("请输入想要发送给服务器的数据：");
        String input = new Scanner(System.in).nextLine();
        out.write(input.getBytes());
        out.flush();

        //读取服务器发来的数据
        BufferedInputStream in = new BufferedInputStream(client.getInputStream());
        for (int i = 0; i < 5; i++) {
            char b = (char)in.read();
            System.out.print(b);
        }
//        int b = 0;
//        while ((b = in.read()) != -1){
//            System.out.print((char) b);
//        }

        //关闭socket
        client.close();
        out.close();
        in.close();
    }
}
